from django.db import models
from django import forms
from django.db import connection, transaction
import MySQLdb as mdb

# Create your models here.
	
class Blog(models.Model):
	author = models.CharField(max_length=50)
	blog_title = models.CharField(max_length=50)
	blog_text = models.TextField()
	pubdate = models.DateField(auto_now=True)
	post_modified = models.DateTimeField(auto_now=True)
	comment_count = models.BigIntegerField()

	def getBlogId(self, blogname):
		blog_name = blogname
		if blog_name is None:
			return 'No title to fetch. Please make sure you are passing the title to the model.'
		else:
			try:
				cursor = connection.cursor()
				cursor.execute("""SELECT id FROM theblog_blog WHERE blog_title=%s""", [blog_name])
				row = cursor.fetchone()	
				cursor.close()
				return '%d' % row
			except Exception as e:
				return "%s" % e
	
	def getContent(self, blogname):
		blog_name = blogname
		if blog_name is None:
			return 'No title to fetch. Please make sure you are passing the title to the model.'
		else:
			try:
				cursor = connection.cursor()
				cursor.execute("""SELECT blog_text, pubdate, comment_count FROM theblog_blog WHERE blog_title=%s""", [blog_name])
				row = cursor.fetchone()	
				cursor.close()
				return row
			except Exception as e:
				return "%s" % e

	def getTitleAndContent(self, blogname):
		blog_name = blogname
		if blog_name is None:
			return 'No title to fetch. Please make sure you are passing the title to the model.'
		else:
			try:
				cursor = connection.cursor()
				cursor.execute("""SELECT blog_title, blog_text FROM theblog_blog WHERE blog_title=%s""", [blog_name])
				row = cursor.fetchone()	
				cursor.close()
				return row
			except Exception as e:
				return "%s" % e
	
	def deleteblog(self, blogname, author):
		blogname = blogname
		author = author
		try:			
			cursor = connection.cursor()
			cursor.execute("""DELETE FROM theblog_blog WHERE blog_title=%s AND author=%s""",[blogname, author])
			cursor.close()
			return 'Blog deleted successfully.'
		except:
			return 'Unable to delete blog.'

	def updateContent(self, new_title, new_content, current_title):
		new_title = new_title
		new_content = new_content
		current_title = current_title
		if new_title is None or new_content is None or current_title is None:
			return 'No data to process.'
		else:				
			try:			
				cursor = connection.cursor()
				cursor.execute("UPDATE theblog_blog SET blog_title=%s, blog_text=%s WHERE blog_title=%s",[new_title, new_content, current_title])
				transaction.commit_unless_managed()
				cursor.close()
				return 'Blog updated successfully.'
			except:
				return 'Unable to update blog.'
	
	def __unicode__(self):
        	return self.blog_title

class Author(models.Model):
	firstname = models.CharField(max_length=50)
	lastname = models.CharField(max_length=50)
	username = models.CharField(max_length=50, primary_key=True)
	password1 = models.CharField(max_length=50)
	password2 = models.CharField(max_length=50)
	email = models.EmailField()
	about_me = models.TextField(blank=True)
	
	def __unicode__(self):
		return self.firstname

class Comments(models.Model):
	comment_author = models.CharField(max_length=50, blank=True)
	comment_date = models.DateTimeField()
	comment_content = models.TextField(blank=True)
	blog = models.ForeignKey(Blog)

	def getComments(self, blogname):
		blog_name = blogname
		if blog_name is None:
			return 'No title to fetch. Please make sure you are passing the title to the model.'
		else:
			try:
				r = []
				cursor = connection.cursor()
				cursor.execute("""select comment_author, comment_date, comment_content from theblog_comments c, theblog_blog b where c.blog_id=b.id and blog_title=%s""", [blog_name])
				row = cursor.fetchall()
				cursor.close()
				return row
			except Exception as e:
				return "%s" % e

	def getNoOfComments(self, blogname):
		if blogname is None:
			return 'No blog title provided'
		else:
			try:
				r = []
				blog_name = blogname
				cursor = connection.cursor()
				cursor.execute("""SELECT count(*) FROM theblog_comments c, theblog_blog b WHERE c.blog_id=b.id AND blog_title=%s""", [blog_name])
				row = cursor.fetchone()
				cursor.close()
				return '%d' % row
			except:
				return 'Unable to return a value'
	
	def __unicode__(self):
		return self.comment_content
		
class Images(models.Model):
	author_image = models.ImageField(upload_to='/myblog/theblog/static/root/images')
	blog_image = models.ImageField(upload_to='/myblog/theblog/static/root/images')
	author = models.ForeignKey(Author)
	blog = models.ForeignKey(Blog)


